package day13;

import java.util.*;


public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            int[] step = new int[m + 1];
            for(int i = 0; i < m+1; i++) {
                step[i] = Integer.MAX_VALUE;
            }
            step[n] = 0;
            for(int i = n; i < m; i++) {
                if(step[i] == Integer.MAX_VALUE) {
                    continue;
                }
                List<Integer> list = div(i);
                //j 代表一次可以跳几块石板
                //i 代表当前石板的编号
                for(int j : list) {
                    if(i+j <= m && step[i] != Integer.MAX_VALUE) {
                        step[i+j] = Math.min(step[i+j],step[i]+1);
                    }else if(i+j <= m) {
                        step[i+j] = step[i] + 1;
                    }
                }

            }
            if(step[m] == Integer.MAX_VALUE) {
                System.out.println(-1);
            }else {
                System.out.println(step[m]);

            }
        }
    }
    public static List<Integer> div(int num) {
        List<Integer> list = new ArrayList<>();
        for(int i = 2; i*i <= num; i++) {
            if(num % i == 0) {
                list.add(i);
                if(num % i != i) {
                    list.add(num/i);
                }
            }
        }
        return list;
    }
}
